// 应用入库
package main

import (
	"flag"
	"github.com/gin-gonic/gin"
	"net/http"
	"organoid_public/app"
	"organoid_public/global"
	"organoid_public/infrastructure/config"
	"os"
	"strconv"
)

func main() {
	//aop := alipay.NewAop()
	//merchantEntity := entity.AlipayCommerceMerchantcardOpenCreateEntity{}
	//PriceInfos := entity.PriceInfosType{
	//	PriceRate:       "0.06",
	//	BackProductCode: "I1140100001000001000",
	//}
	//SettleInMerchantLicense := entity.SettleInMerchantLicenseType{
	//	Phone: "17726216129",
	//	Name:  "勤鸟测试",
	//}
	//merchantEntity.MerchantName = "重庆致君行企业管理咨询有限公司"
	//merchantEntity.Pid = "2088541588747012"
	//merchantEntity.CardTypes = []string{"AXF_MERCHANT_PERIOD_PAY"}
	//merchantEntity.NeedAuth = true
	//merchantEntity.PriceInfos = []*entity.PriceInfosType{&PriceInfos}
	//merchantEntity.SettleInMerchantLicense = &SettleInMerchantLicense
	//result := new(dto.AlipayCommerceMerchantcardOpenCreateDto)
	//aop.Execute(&merchantEntity, result)
	//fmt.Println(result)
	//return
	var (
		err        error
		configPath string
	)

	flag.StringVar(&configPath, "config", "etc/", "配置文件路径")
	flag.Parse()
	conf := config.LoadConfig(configPath)
	global.InitGlobal(conf)
	//g := gin.Default()
	g := gin.New()
	//注册全局中间件，日志和异常捕获
	g.Use(gin.Logger(), gin.RecoveryWithWriter(os.Stderr, func(c *gin.Context, errs any) {
		c.Status(http.StatusOK)
		if eror, ok := errs.(error); ok {
			c.JSON(http.StatusOK, map[string]string{"msg": eror.Error(), "code": strconv.Itoa(http.StatusBadRequest)})
		} else {
			c.JSON(http.StatusOK, map[string]string{"msg": errs.(string), "code": strconv.Itoa(http.StatusBadRequest)})
		}
		c.Abort()
	}))
	err = g.SetTrustedProxies(global.AppConfig.Server.Proxies)
	if err != nil {
		panic(err)
	}

	app.LoadRoutes(g)

	err = g.Run(global.AppConfig.Server.Address)
	if err != nil {
		panic(err)
	}
}
